package com.hfh.test;

/**
 * @Author hfh
 * @Date 2022 09 15 11 25
 **/
public class SellTickets implements Runnable{
    private int num = 100;

    public static void main(String[] args) {
        SellTickets st = new SellTickets();
        for (int i = 1; i <= 5; i++){
            new Thread(st,"窗口"+i+":").start();
        }

    }

    public void run1() {
        while (num > 0) {

            synchronized (this) {
                if (num > 0) {
                    System.out.println(Thread.currentThread().getName() + ":售出第" + num + "张票");
                    num--;
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    @Override
    public void run() {
        for (int i = num;i>0;i--){
            synchronized (SellTickets.class){
                if (num>0){
                    System.out.println(Thread.currentThread().getName()+"卖出第"+num+"张票");
                    num--;
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }
}

